package StudyCode.summary.Basic.mymath;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class DistanceLongitudeLatitude {

	public static void main(String[] args) {
		System.out.println("经纬度(10,10)跟经纬度(20,20)之间的距离是(单位:米):" + getDistance(10, 10, 20, 20));
	}

	public static double getDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
		double radLat1 = rad(latitude1);
		double radLat2 = rad(latitude2);
		double a = radLat1 - radLat2;
		double b = rad(longitude1) - rad(longitude2);
		double c = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
		c = c * 6378.137;
		return (Math.round(c * 10000d) / 10000d) * 1000;
	}

	private static double rad(double d) {
		return d * Math.PI / 180.0;
	}
}
